ModEditor.Camera.Current = 0
-- 0 = 3D
-- 1 = Top
-- 2 = Side
-- 3 = Front
ModEditor.Camera.Position = {0,0,0}
ModEditor.Camera.Distance = 10
ModEditor.Camera.Angle = 0

function ModEditor.Camera.ApplyCamera(id)
	local positions = {}
	for i = 1,#ModEditor.Windows.ObjectControl.ObjectList.Items do
		if ModEditor.Windows.ObjectControl.ObjectList.Items[i][2] == true then
			for d = 1,#ModEditor.ObjectList do
				if ModEditor.ObjectList[d].Name == ModEditor.Windows.ObjectControl.ObjectList.Items[i][1] then
					table.insert(positions,{ModEditor.ObjectList[d].Pos[1],ModEditor.ObjectList[d].Pos[2],ModEditor.ObjectList[d].Pos[3]})
				end
			end
		end
	end
	local center = {0,0,0}
	if #positions == 0 then
		local camera_info = get_camera_info()
		center = {camera_info.lookat.x,camera_info.lookat.y,camera_info.lookat.z}
	else
		for i = 1,#positions do
			center[1] = center[1] + positions[i][1]
			center[2] = center[2] + positions[i][2]
			center[3] = center[3] + positions[i][3]
		end
		center[1] = center[1] / #positions
		center[2] = center[2] / #positions
		center[3] = center[3] / #positions
	end
	ModEditor.Camera.Position = center
	ModEditor.Camera.Current = id
end

ModEditor.Camera.ApplyCamera(0)

ModEditor.Windows.CameraControl = ModEditor.GUI.BasicWindow:New()
ModEditor.Windows.CameraControl:Apply({{"Caption","Camera Control"},{"X",500},{"Y",10},{"Width",135},{"Height",150},{"ShowFullWindow",true},{"Visible",true}})

ModEditor.Windows.CameraControl.TopView = ModEditor.GUI.CheckBox:New(ModEditor.Windows.CameraControl)
ModEditor.Windows.CameraControl.TopView:Apply({{"X",10},{"Y",30},{"Width",ModEditor.Windows.CameraControl.Width-20},{"Height",20},{"Caption","Top"},{"Checked",false}})

ModEditor.Windows.CameraControl.SideView = ModEditor.GUI.CheckBox:New(ModEditor.Windows.CameraControl)
ModEditor.Windows.CameraControl.SideView:Apply({{"X",10},{"Y",60},{"Width",ModEditor.Windows.CameraControl.Width-20},{"Height",20},{"Caption","Side"},{"Checked",false}})

ModEditor.Windows.CameraControl.FrontView = ModEditor.GUI.CheckBox:New(ModEditor.Windows.CameraControl)
ModEditor.Windows.CameraControl.FrontView:Apply({{"X",10},{"Y",90},{"Width",ModEditor.Windows.CameraControl.Width-20},{"Height",20},{"Caption","Front"},{"Checked",false}})

ModEditor.Windows.CameraControl.View3D = ModEditor.GUI.CheckBox:New(ModEditor.Windows.CameraControl)
ModEditor.Windows.CameraControl.View3D:Apply({{"X",10},{"Y",120},{"Width",ModEditor.Windows.CameraControl.Width-20},{"Height",20},{"Caption","3D"},{"Checked",true}})

function ModEditor.Windows.CameraControl.TopView:OnCheck()
	if self.Checked == false then
		self.Checked = true
	else
		ModEditor.Windows.CameraControl.SideView.Checked = false
		ModEditor.Windows.CameraControl.FrontView.Checked = false
		ModEditor.Windows.CameraControl.View3D.Checked = false
		ModEditor.Camera.ApplyCamera(1)
	end
end
function ModEditor.Windows.CameraControl.SideView:OnCheck()
	if self.Checked == false then
		self.Checked = true
	else
		ModEditor.Windows.CameraControl.TopView.Checked = false
		ModEditor.Windows.CameraControl.FrontView.Checked = false
		ModEditor.Windows.CameraControl.View3D.Checked = false
		ModEditor.Camera.ApplyCamera(2)
	end
end
function ModEditor.Windows.CameraControl.FrontView:OnCheck()
	if self.Checked == false then
		self.Checked = true
	else
		ModEditor.Windows.CameraControl.SideView.Checked = false
		ModEditor.Windows.CameraControl.TopView.Checked = false
		ModEditor.Windows.CameraControl.View3D.Checked = false
		ModEditor.Camera.ApplyCamera(3)
	end
end
function ModEditor.Windows.CameraControl.View3D:OnCheck()
	if self.Checked == false then
		self.Checked = true
	else
		ModEditor.Windows.CameraControl.SideView.Checked = false
		ModEditor.Windows.CameraControl.FrontView.Checked = false
		ModEditor.Windows.CameraControl.TopView.Checked = false
		ModEditor.Camera.ApplyCamera(0)
	end
end
ModEditor.Windows.Add(ModEditor.Windows.CameraControl)
function ModEditor.Camera.Update()
	if ModEditor.Camera.Current == 0 then
		return
	elseif ModEditor.Camera.Current == 1 then
		set_camera_lookat(ModEditor.Camera.Position[1],ModEditor.Camera.Position[2],ModEditor.Camera.Position[3])
		set_camera_pos(ModEditor.Camera.Position[1]+math.sin(math.pi/180*ModEditor.Camera.Angle)*0.01,ModEditor.Camera.Position[2]+math.cos(math.pi/180*ModEditor.Camera.Angle)*0.01,ModEditor.Camera.Position[3]+ModEditor.Camera.Distance)
	elseif ModEditor.Camera.Current == 2 then
		set_camera_lookat(ModEditor.Camera.Position[1],ModEditor.Camera.Position[2],ModEditor.Camera.Position[3])
		set_camera_pos(ModEditor.Camera.Position[1]+ModEditor.Camera.Distance,ModEditor.Camera.Position[2],ModEditor.Camera.Position[3])
	elseif ModEditor.Camera.Current == 3 then
		set_camera_lookat(ModEditor.Camera.Position[1],ModEditor.Camera.Position[2],ModEditor.Camera.Position[3])
		set_camera_pos(ModEditor.Camera.Position[1],ModEditor.Camera.Position[2]+ModEditor.Camera.Distance,ModEditor.Camera.Position[3])
	end
end